import { defineStore } from "pinia"
import { onMounted, ref } from "vue"
import { get, post } from "@/utils/api"
import { MessagePlugin } from "tdesign-vue-next"

export const useAdminRecycleAuthorizationStore = defineStore("adminRecycleAuthorization", () => {
  onMounted(async () => {
    await table.getData()
  })
  // 表格模块
  const table = {
    data: ref([]),
    getData: async () => {
      table.loading.value = true
      try {
        table.data.value = await get("/api/authorization/getAllWithDeleted")
      } catch (err) {
        console.log(err)
      } finally {
        table.loading.value = false
      }
    },
    loading: ref(false),
    key: ref(0),
    searchData: ref(""),
    search: async () => {
      table.loading.value = true
      table.data.value = await post("/api/authorization/searchWithDeleted", { body: table.searchData.value })
      table.loading.value = false
    },
    refresh: async () => {
      await table.getData()
      table.key.value++
      table.searchData.value = ""
    }
  }

  // 删除模块
  const deleteDialog = {
    id: ref(),
    ids: ref([]),
    idsChange: (value: any) => {
      deleteDialog.ids.value = value
    },
    visible: ref(),
    groupVisible: ref(),
    loading: ref(false),
    groupLoading: ref(),
    delete: (id: number | string) => {
      deleteDialog.visible.value = true
      deleteDialog.id.value = id
    },
    confirmDelete: async () => {
      deleteDialog.loading.value = true
      try {
        const response = await post("/api/authorization/remove", { id: deleteDialog.id.value })
        MessagePlugin(response.status, response.message)
        await table.getData()
        table.key.value++ // 重新渲染表格
      } catch (err) {
        console.log(err)
        await MessagePlugin("error", "系统错误，请稍后再试~")
      } finally {
        deleteDialog.loading.value = false
        deleteDialog.visible.value = false
      }
    },
    isGroupEmpty: async () => {
      if( deleteDialog.ids.value.length === 0){
        await MessagePlugin("error", "删除失败，数据为空！")
      }else{
        deleteDialog.groupVisible.value = true
      }
    },
    confirmDeleteGroup: async () => {
      try {
        const response = await post("/api/authorization/removeGroup", { ids: deleteDialog.ids.value })
        MessagePlugin(response.status, response.message)
        await table.getData()
        table.key.value++ // 重新渲染表格
      } catch (err) {
        console.log(err)
        await MessagePlugin("error", "系统错误，请稍后再试~")
      } finally {
        deleteDialog.groupLoading.value = false
        deleteDialog.groupVisible.value = false
      }
    }

  }
  // 恢复模块
  const restoreDialog = {
    visible: ref(false),
    loading: ref(false),
    restoreData: ref(),
    confirmRestore: async () => {
      restoreDialog.loading.value = true
      try {
        const response = await post("/api/authorization/restore", restoreDialog.restoreData.value )
        MessagePlugin(response.status, response.message)
        await table.getData()
        table.key.value++ // 重新渲染表格
      } catch (err){
        console.log(err)
        await MessagePlugin("error", "系统错误，请稍后再试~")
      } finally {
        restoreDialog.loading.value = false
        restoreDialog.visible.value = false
      }
    },
    restore: (value: any) =>{
      restoreDialog.visible.value = true
      restoreDialog.restoreData.value = value
    }
  }
  return {
    table,
    restoreDialog,
    deleteDialog
  }
})
